import CryptoJS from "crypto-js";
// 引入全局utils
import XEUtils from 'xe-utils'
import router from '../router'
import clipboard from 'vue-clipboard3'

const modules = import.meta.glob('/src/views/*/*/*.vue')

const tool = {
  isEmpty(value) {
    if (value == null || value === '' || typeof value === 'undefined') {
      return true;
    } else {
      return false;
    }
  },
  isNotEmpty(val) {
    if (val == null || val === '' || typeof val === 'undefined') {
      return false;
    } else {
      return true;
    }
  },

  // 常用JS加解密
  crypto: {
    MD5 (data) {
      const SALT = "ZLLOVEZLT@1314!@"; // 盐值
      return CryptoJS.MD5(data + SALT).toString()
    },
    BASE64: {
      encrypt (data) {
        return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
      }
    }
  },

  // 格式化工具
  format: {
    /**
     * 将时间戳转化成yyyy-MM-dd格式数据
     * @param timestamp 
     */
    formatDate (timestamp) {
      return XEUtils.toDateString(timestamp, 'yyyy-MM-dd')
    },
    formatTime (timestamp) {
      return XEUtils.toDateString(timestamp, 'yyyy-MM-dd HH:mm:ss')
    }
  },

  // localStorage 相关方法
  data: {
    set(key, data) {
      return localStorage.setItem(key, JSON.stringify(data));
    },
    get(key) {
      try {
        let value = localStorage.getItem(key);
        if (value) {
          value = JSON.parse(value);
          return value;
        } else {
          return null;
        }
      } catch (error) {
        return null;
      }
    },
    remove(key) {
      return localStorage.removeItem(key);
    },
    clear() {
      return localStorage.clear();
    }
  },

  // sessionStorage 相关方法
  session: {
    set (table, data) {
      const _data = JSON.stringify(data);
      return sessionStorage.setItem(table, _data)
    },
    get (table) {
      let data = sessionStorage.getItem(table)
      if (!data) { 
        return null
      }
      return data
    },
    remove (table) {
      return sessionStorage.removeItem(table)
    },
    clear () { 
      return sessionStorage.clear()
    }
  },

  /**
   * 对象赋值
   * @param {赋值来源} from 
   * @param {赋值实体} to 
   */
  assionValues (from, to) {
    const modelKeys = Object.keys(to)
    for (const key in from) {
      if (modelKeys.includes(key) && to.hasOwnProperty(key)) { 
        to[key] = from[key]
      }
    }
  },

  /**
   * 对象清除
   * @param {传入的对象} obj 
   */
  clearObject (obj) {
    if (typeof obj === 'string') {
      return ''
    } else if (typeof obj === 'number') {
      return 0
    } else if (typeof obj === 'boolean') {
      return false
    } else if (typeof obj === 'object') {
      if (Array.isArray(obj)) {
        return obj.map(this.clearObject)
      } else {
        const result = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            result[key] = this.clearObject(obj[key])
          }
        }
        return result
      }
    }
    return obj
  },

  /**
   * 清除空属性
   */
  removeEmptyProperties (obj) {
    for (let key in obj) {
        if (obj[key] === '' || obj[key] === null) {
            delete obj[key];
        }
    }
  },

  /**
   * 循环处理树状菜单
   * @param {菜单列表} menus 
   * @param {父级菜单} parent 
   */
  dealWithRoute (menus, parent = 'Layout') {
    if (!menus || menus.length == 0) {
      return;
    }
    for (let item of menus) {
      // 多级菜单
      if (item.children && item.children.length > 0) {
        router.addRoute(parent, {
          path: item.path,
          name: item.name,
          component: modules[`/${item.url}`],
          mete: item.mate
        })
        this.dealWithRoute(item.children, item.path.split('/')[1])
      } else { // 一级菜单
        router.addRoute(parent, {
          path: item.path,
          name: item.name,
          component: modules[`/${item.url}`],
          mete: item.mate
        })
      }
    }
  },

  /**
   * 将字符串复制到剪贴板
   * @param {复制的内容} str 
   * @returns 
   */
  async copy (str) {
    const { toClipboard } = clipboard()
    try {
      await toClipboard(str)
      return true
    } catch (error) {
      alert('复制失败')
      return false
    }
  }
}

export default tool